package com.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.demo.common.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.Role;
import com.demo.service.IRoleService;
import com.demo.common.dto.PageDTO;
import com.demo.dto.RoleDTO;
import com.demo.query.RoleQueryDTO;
import com.demo.vo.RoleVO;

import java.util.List;

@Tag(name = "角色表管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final IRoleService roleService;

    public RoleController(IRoleService roleService) {
        this.roleService = roleService;
    }

    @SaCheckPermission("btn.queryRole")
    @Operation(summary = "查询所有角色表信息")
    @GetMapping("list")
    public SaResult list() {
        List<Role> roleList = roleService.list();
        List<RoleVO> result = BeanUtil.copyToList(roleList, RoleVO.class);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.queryRole")
    @Operation(summary = "分页查询角色表信息")
    @PostMapping("page")
    public SaResult page(@RequestBody RoleQueryDTO roleQueryDTO) {
        Integer currentPage = roleQueryDTO.getCurrentPage();
        Integer pageSize = roleQueryDTO.getPageSize();

        LambdaQueryWrapper<Role> queryWrapper = roleQueryDTO.buildQueryWrapper();
        Page<Role> rolePage = roleService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<RoleVO> rolePageVO = PageDTO.getPageVO(rolePage, item -> BeanUtil.copyProperties(item, RoleVO.class));
        return SaResult.ok().setData(rolePageVO);
    }

    @SaCheckPermission(value = {"btn.addRole", "btn.editRole"}, mode = SaMode.OR)
    @Operation(summary = "新增/修改角色表信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody RoleDTO roleDTO) {
        Role role = BeanUtil.copyProperties(roleDTO, Role.class);
        boolean result = roleService.saveOrUpdate(role);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.queryRole")
    @Operation(summary = "根据id获取角色表信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "角色表id")
                            @PathVariable Integer id) {
        Role role = roleService.getById(id);
        if (role == null) {
            return SaResult.error("数据不存在");
        }
        RoleVO result = BeanUtil.copyProperties(role, RoleVO.class);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.deleteRole")
    @Operation(summary = "根据id删除角色表信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "角色表id")
                           @PathVariable Integer id) {
        boolean result = roleService.removeById(id);
        return SaResult.ok(result ? "删除成功" : "删除失败").setData(result);
    }

    @SaCheckPermission("btn.deleteRole")
    @Operation(summary = "根据id批量删除角色表信息")
    @DeleteMapping("/removeBatch/{ids}")
    public SaResult removeBatch(@Parameter(description = "id集合")
                                @PathVariable List<Integer> ids) {
        boolean result = roleService.removeByIds(ids);
        return SaResult.ok(result ? "删除成功" : "删除失败").setData(result);
    }
}
